﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.CRM;
using Me.Amon.Da.Db.POS;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class Shops : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_06)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();
            dba.AddTable(DCompany.TABLE);
            dba.AddColumn(DCompany.CODE);
            dba.AddColumn(DCompany.NAME);
            dba.AddColumn(DCompany.ADDRESS);
            var dt = dba.ExecuteSelect();
            var cnt = dt.Rows.Count;

            string name;
            string code;
            for (int i = 0; i < cnt; i += 1)
            {
                name = dt.Rows[i][DCompany.NAME] as string;
                code = dt.Rows[i][DCompany.CODE] as string;
                LbCompany.Items.Add(new ListItem { Text = name, Value = code });
            }

            var corp = Request["corp"];
            if (!CharUtil.IsValidateCode(corp))
            {
                corp = (cnt > 0) ? (dt.Rows[0][DCompany.CODE] as string) : "0";
            }
            LbCompany.SelectedValue = corp;

            dba.Reset();
            ListShop(dba, corp, 0);
        }

        protected void LbCompany_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListShop(new RDBEngine(), LbCompany.SelectedValue, 0);
        }

        #region 导航
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnAppendShop_Click(object sender, EventArgs e)
        {
            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_02)//IUser.LEVEL_07
            {
                LlErr.Text = string.Format("权限不足，请联系管理员！");
                return;
            }
            Response.Redirect("~/POS/UpdateShop.aspx?corp=" + LbCompany.SelectedValue);
        }
        #endregion

        private void ListShop(RDBEngine dba, string corp, int page)
        {
            dba.AddTable(DShop.TABLE);
            dba.AddColumn(DShop.ID);
            dba.AddColumn(DShop.CODE);
            dba.AddColumn(DShop.CORP_ID);
            dba.AddColumn(DShop.NAME);
            dba.AddColumn(DShop.ADDRESS);
            dba.AddColumn(DShop.LEVEL);
            dba.AddColumn(DShop.POSTCODE);
            dba.AddColumn(DShop.CONTACT);
            dba.AddColumn(DShop.CELLPHONE);
            dba.AddColumn(DShop.TELEPHONE);
            dba.AddColumn(DShop.STATUS);
            dba.AddWhere(DShop.CORP_ID, corp);

            var table = dba.ExecuteSelect();
            int cnt = table.Rows.Count;

            // 每页行数
            int RPP = 20;
            int num = (cnt - 1) / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpShops.DataSource = pds;
            RpShops.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            ListShop(new RDBEngine(), LbCompany.SelectedValue, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            ListShop(new RDBEngine(), LbCompany.SelectedValue, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            ListShop(new RDBEngine(), LbCompany.SelectedValue, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            ListShop(new RDBEngine(),LbCompany.SelectedValue, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            ListShop(new RDBEngine(), LbCompany.SelectedValue, max);
        }
        #endregion
    }
}